package LeetCode_114;


import java.util.ArrayList;
import java.util.List;


/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/24 20:07
 */
public class Solution {
    public static void main(String[] args) {
        TreeNode n1 = new TreeNode(1);
        TreeNode n2 = new TreeNode(2);
        TreeNode n3 = new TreeNode(3);
        TreeNode n4 = new TreeNode(4);
        TreeNode n5 = new TreeNode(5);
        TreeNode n6 = new TreeNode(6);
        TreeNode n7 = new TreeNode(7);
        TreeNode n8 = new TreeNode(8);
        TreeNode n9 = new TreeNode(9);
        TreeNode n10 = new TreeNode(10);
        TreeNode n11 = new TreeNode(11);

        n1.left = n2;   n1.right = n3;
        n2.left = n4;   n2.right = n5;
        n3.left = null; n3.right = n6;
        n4.left = null; n4.right = null;
        n5.left = n7;   n5.right = n8;
        n6.left = n9;   n6.right = null;
        n9.left = n10;  n9.right = n11;

        Solution solution = new Solution();
        List<Integer> list = solution.preorderTraversal(n1);
        System.out.println(list);
    }
    public List<Integer> preorderTraversal(TreeNode root) {
        //2种情况
        //1.空树 返回 Empty List<Integer>
        List<Integer> ans = new ArrayList<>() ;
        if (root == null) {
            return ans;
        }
        //2.非空
        //1)处理当前的根
        ans.add(root.val);
        //2）左，注意返回值
        ans.addAll(preorderTraversal(root.left));
        //3)右
        ans.addAll(preorderTraversal(root.right));
        return ans;
    }
}
